#include <stdio.h>
#include <stdlib.h>
#include "solution.h"
#include "edgeGraph.h"
#include "graphbmp-tools.h"

#define CAMION_0 0
#define CAMION_1 1

int main(){

	int K=2;
	int N=10;
	printf("creo la soluzione\n");
	Psolution sol=initSolution(&K,&N);
	PedgeID _21=initEdgeID(0,1);
	PedgeGraph g21=initEdgeGraph(1,2,5);
	validateEdgeID(_21);
	PedgeID _23=initEdgeID(1,2);
	PedgeGraph g23=initEdgeGraph(3,4,5);
	validateEdgeID(_23);
	PedgeID _34=initEdgeID(2,3);
	PedgeGraph g34=initEdgeGraph(5,6,5);
	validateEdgeID(_34);
	PedgeID _78=initEdgeID(6,7);
	PedgeGraph g78=initEdgeGraph(6,1,5);
	validateEdgeID(_78);
	PedgeID _24=initEdgeID(3,1);
	PedgeGraph g24=initEdgeGraph(6,5,5);
	validateEdgeID(_24);
	PedgeID _45=initEdgeID(3,4);
	PedgeGraph g45=initEdgeGraph(6,5,5);
	validateEdgeID(_45);
	PedgeID _56=initEdgeID(4,5);
	PedgeGraph g56=initEdgeGraph(6,5,5);
	validateEdgeID(_56);
	PedgeID _46=initEdgeID(5,3);
	PedgeGraph g46=initEdgeGraph(6,5,5);
	validateEdgeID(_46);


	printSolution(stdout,sol);
	printf("aggiungo gli archi\n");
	addEdgeToVehicleState(_21,g21,getVehicleStateInSolution(sol,CAMION_0),true);
	tryToGatherEdge(sol->gMatrix,_21,CAMION_0);
	addEdgeToVehicleState(_21,g21,getVehicleStateInSolution(sol,CAMION_0),true);
	addEdgeToVehicleState(_23,g23,getVehicleStateInSolution(sol,CAMION_0),true);
	tryToGatherEdge(sol->gMatrix,_23,CAMION_0);
	addEdgeToVehicleState(_34,g34,getVehicleStateInSolution(sol,CAMION_0),true);
	tryToGatherEdge(sol->gMatrix,_34,CAMION_0);
	addEdgeToVehicleState(_24,g24,getVehicleStateInSolution(sol,CAMION_0),true);
	tryToGatherEdge(sol->gMatrix,_24,CAMION_0);
	addEdgeToVehicleState(_45,g45,getVehicleStateInSolution(sol,CAMION_0),true);
	tryToGatherEdge(sol->gMatrix,_45,CAMION_0);
	addEdgeToVehicleState(_56,g56,getVehicleStateInSolution(sol,CAMION_0),true);
	tryToGatherEdge(sol->gMatrix,_56,CAMION_0);
	addEdgeToVehicleState(_46,g46,getVehicleStateInSolution(sol,CAMION_0),true);
	tryToGatherEdge(sol->gMatrix,_46,CAMION_0);
	//addEdgeInVehicleState(_78,g78,getVehicleStateInSolution(sol,CAMION_0),true);
	//tryToGatherEdge(sol->gMatrix,_78,CAMION_0);

	/*printf("stampo la soluzione\n");
	printSolution(stdout,sol);
	printf("elimino l'arco (1,2) (doppio)\n");
	removeEdgeFromVehicleState(_21,g21,getVehicleStateInSolution(sol,CAMION_0),true);
	printf("elimino l'arco (3,4)\n");
	removeEdgeFromVehicleState(_34,g34,getVehicleStateInSolution(sol,CAMION_0),true);*/

	printSolution(stdout, sol);

	/*printf("chi soddisfa l'arco (21)? ");
	printf("%d\n",whoGathersEdge(sol->gMatrix,_21)+1);
	printf("chi soddisfa l'arco (34)? ");
	printf("%d\n",whoGathersEdge(sol->gMatrix,_34)+1);
	printf("chi soddisfa l'arco (78)? ");
	printf("%d\n",whoGathersEdge(sol->gMatrix,_78)+1);*/

	printf("ora provo a usare le primitive per scorrere la soluzione:\n");
	printf("partom:\n");
	bool start=true;
	PedgeSol edge;
	PvehiclePath path=getVehiclePathInSolution(sol,0);
	resetMemory(path,DIRECTION_NEXT);
	while((edge=getNearEdgeInVehiclePath(path,DIRECTION_NEXT))!=NULL){
		start=false;
		printf("chi soddisfa l'arco (%d;%d) usando \"whoGathersEdge\"? camion %d",edge->id->i,edge->id->j,whoGathersEdge(sol->gMatrix,edge->id));
		printEdgeSol(stdout, edge);
		printf("\n");
	}

	printf("ora guardo quali sono gli archi adiacenti ad un nodo 1\n");
	PvehiclePath adiacenti1=getEdgesNearNodeInVehiclePath(path,0);
	printVehiclePath(stdout, adiacenti1);
	printf("ora guardo quali sono gli archi adiacenti ad un nodo 3\n");
	PvehiclePath adiacenti3=getEdgesNearNodeInVehiclePath(path,2);
	printVehiclePath(stdout,adiacenti3);
	printf("ora guardo quali sono gli archi adiacenti ad un nodo 4\n");
	PvehiclePath adiacenti4=getEdgesNearNodeInVehiclePath(path,3);
	printVehiclePath(stdout, adiacenti4);

	printf("controllo ora che il path sia connesso\n");
	printf("il grafo e' %s\n", isConnected(sol->states[CAMION_0]->pvp, sol->states[CAMION_0]->lEdges)==1?"connesso":"non connesso");
	printf("provo ora ad fare un \"remove\" del nodo 4!\n");

	removeEdgeFromVehiclePath(_24, getVehicleStateInSolution(sol,CAMION_0)->pvp);
	removeEdgeFromVehiclePath(_34,getVehicleStateInSolution(sol,CAMION_0)->pvp);
	addEdgeToVehiclePath(_23, getVehicleStateInSolution(sol,CAMION_0)->pvp);

	//tryToGatherEdge(sol->gMatrix,_23,CAMION_0);

	printSolution(stdout,sol);
	printf("disegno la soluzione per una migliore comprensione\n");
	drawSolutionFromScratchDefaults("solutiontest.bmp",sol,0);

	printf("il grafo e' %s\n", isConnected(sol->states[CAMION_0]->pvp, sol->states[CAMION_0]->lEdges)==true?"connesso":"non connesso");

	printf("provo ora a liberare un VehiclePath!\n");
	printVehiclePath(stdout, path);
	freeVehiclePathHard(path);
	printVehiclePath(stdout, path);
	printf("fine.\n");

}
